import { _decorator, Component, instantiate, Label, Node, Prefab, resources } from 'cc';
import { lobby } from '../../lobby';
const { ccclass, property } = _decorator;

export function showAlert(title: string, content: string, cb?: Function) {
    let prefab = resources.get("prefab/alert", Prefab)!
    let alert = instantiate(prefab)
    lobby.instance.alert.addChild(alert);
    alert.getComponent(Alert)?.showAlert(title, content, cb)
}

@ccclass('alert')
export class Alert extends Component {
    @property(Label)
    title!: Label;

    @property(Label)
    content!: Label;

    @property(Node)
    btnok!: Node;

    @property(Node)
    btncancel!: Node;

    showAlert(title: string, content: string, cb?: Function) {
        this.title.string = title
        this.content.string = content
        this.btnok.on(Node.EventType.TOUCH_END, () => {
            this.node.destroy()
            cb && cb(1)
        })
        this.btncancel.on(Node.EventType.TOUCH_END, () => {
            this.node.destroy()
            cb && cb(0)
        })
    }

    update(deltaTime: number) {

    }
}


